<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加</title>
    <script src="/jquery/jquery-3.2.1.min.js"></script>
    <script src="/layui/layui.js"></script>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/css/course/courseManage/courseList/add-course.css">
    <script src="/js/public/public.js"></script>
</head>
<body>
<div class="layui-row">
    <div class="layui-col-md12">
        <div class="list">
            <div class="list-left layui-col-md8">
                <div class="list-left-left"></div>
                <p class="list-left-right" id="title">添加后台人员</p>
            </div>
            <div class="list-right layui-col-md4">
                <div class="functionButton">
                    <button class="list-right-left layui-btn" style="margin-top: 3px" onclick="refresh()">
                        <i class="layui-icon layui-icon-refresh"></i>&nbsp;刷新
                    </button>
                    <button id="goBack" class="list-right-right layui-btn" style="margin-top: 3px">
                        <i class="layui-icon layui-icon-left"></i>&nbsp;返回
                    </button>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-col-md12">
        <div class="courseForm">
            <form class="layui-form">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">用户姓名:</label>
                        <div class="layui-input-inline">
                            <input name="trueName" id="trueName" placeholder="请输入用户姓名" class="layui-input" lay-verify="required" />
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">用户账号:</label>
                        <div class="layui-input-inline">
                            <input name="account" id="account" placeholder="请输入用户账号" class="layui-input" lay-verify="required|account" />
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">选择图片:</label>
                        <div class="layui-input-inline">
                            <button type="button" id="selectHeadPortraitImg" class="layui-btn">选择图片</button>
                            <input type="hidden" name="acatarUrl" lay-verify="acatarUrl" id="acatarUrl">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <div class="layui-progress layui-progress-big" lay-filter="imgProgress" lay-showPercent="yes">
                                <div class="layui-progress-bar layui-bg-green" lay-percent="0%"></div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">预览图:</label>
                        <div class="layui-input-inline">
                            <img src="http://101.200.239.240:80/group1/M00/00/23/rBmy-mIMW2GAQTWGAAAHnMGty5E715.jpg" id="userHeadPortrait">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline" style="margin-left: -66px">
                        <label class="layui-form-label">性别:</label>
                        <input type="radio" name="sex" value="男" title="男" checked lay-filter="radio">
                        <input type="radio" name="sex" value="女" title="女" lay-filter="radio">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">邮箱地址:</label>
                        <div class="layui-input-inline">
                            <input name="email" id="email" placeholder="请输入邮箱地址" class="layui-input" lay-verify="required|email" />
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">密码:</label>
                        <div class="layui-input-inline">
                            <input name="password" id="password" type="password" placeholder="请输入密码" class="layui-input" lay-verify="required|pass" />
                            <span style="color: red;">密码由数字、字母两种字符组成，长度在8-15位之间</span>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">手机号:</label>
                        <div class="layui-input-inline">
                            <input name="phone" id="phone"  placeholder="请输入手机号" class="layui-input" lay-verify="required|phone" />
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">身份证号:</label>
                        <div class="layui-input-inline">
                            <input name="idCard" id="idCard"  placeholder="请输入身份证号" class="layui-input" lay-verify="required|identity" />
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">出生日期:</label>
                        <div class="layui-input-inline">
                            <input name="birthDate" id="birthDate"  placeholder="请输入出生日期" class="layui-input" lay-verify="required" />
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">个性签名:</label>
                        <div class="layui-input-inline">
                            <input name="freestyle" id="freestyle"  placeholder="请输入个性签名" class="layui-input" lay-verify="required" />
                        </div>
                    </div>
                </div>
                <div class="layui-form-item" style="display: inline-block" >
                    <label class="layui-form-label">选择部门：</label>
                    <div class="layui-input-inline">
                        <select class="layui-select" lay-verify="required" name="dept" id="dept" lay-filter="dept">
                            <option value="">请选择</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label"></label>
                        <div class="layui-input-inline">
                            <button type="submit" class="layui-btn" id="submit" lay-filter="sub" lay-submit>添加后台人员</button>
                        </div>
                    </div>
                </div>
                <input type="hidden" name="remark" value="后台添加">
                <input type="hidden" name="enabled" value="1">
                <input type="hidden" name="status" value="1">
                <input type="hidden" name="grade" value="1">
                <input type="hidden" name="wages" value="2000">
                <input type="hidden" name="registerIp" id="registerIp">
            </form>
        </div>
    </div>
</div>
</body>
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
<script>
    layui.use(['jquery','layer','form','laydate','upload','element'],function() {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var laydate=layui.laydate;
        var upload=layui.upload;
        var element=layui.element;
        $('#registerIp').val(returnCitySN.cip);
        //出生日期渲染
        laydate.render({
            elem: '#birthDate',
            type: 'datetime',
            trigger: 'click'
        })
        //部门分类渲染
        $.ajax({
            url: '/login/getAllJsDept',
            dataType: 'json',
            type: 'post',
            data:{},
            success: function (data) {
                for (var i = 0; i < data.data.length; i++) {
                    $('#dept').append('<option value="' + data.data[i].id + '" >' + data.data[i].deptName + '</option>')
                }
                form.render('select');
            }
        });
        //刷新功能
        refresh=function refresh(){
            location.reload();
        }
        var imgFiles;
        var imgIndex;
        var imgUploadCount = 0;
        //上传头像
        upload.render({
            elem:"#selectHeadPortraitImg",
            url:"/login/uploadHeadPortrait",
            field:'headPortrait',
            accept:'images',
            size: 2048,
            choose: function (obj) {
                imgUploadCount++;
                obj.preview(function(index, file, result){
                    imgIndex = index;
                    $('#userHeadPortrait').attr('src',result);
                });
                if(imgUploadCount != 1){
                    delete imgFiles[imgIndex];
                }
                imgFiles = obj.pushFile();
                element.progress('imgProgress', '99%');
            },
            done:function(res){
                if(res.data.code == 200){
                    $('#acatarUrl').val(res.data.src);
                    element.progress('imgProgress', '100%');
                    layer.msg('头像上传成功');
                }
            }
        })

        form.verify({
            acatarUrl:function (value, item) {
                if(value == ''){
                    return '请上传头像';
                }
            },
            pass:function (value, item) {
                var password = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,15}$/;
                if(!password.test(value)){
                    return '密码格式不正确';
                }
            },
            account: function (value ,item) {
                var usersAccount = getJsUsersAccount();
                if(usersAccount.length > 0){
                    for (var i = 0; i < usersAccount.length; i++) {
                        if(value == usersAccount[i]){
                            return '用户账户不可以重复';
                        }
                    }
                }
            }
        });
        function getJsUsersAccount() {
            var usersAccount;
            $.ajax({
                url: "/login/getJsUserAccount",
                dataType: "json",
                async: false,
                success:function (data) {
                    usersAccount = data.data;
                }
            })
            return usersAccount;
        }
        var deptAuthoritys;
        form.on('select(dept)', function(data){
            getUserSelectDeptAuthority(data.value);
        });
        //根据部门id查询部门权限
        function getUserSelectDeptAuthority(deptId){
            $.ajax({
                url: '/jsDeptAuthority/getUserSelectDeptAuthority',
                dataType: 'json',
                data: {deptId: deptId},
                success: function (data) {
                    deptAuthoritys = data.data;
                }
            });
        }

        form.on('submit(sub)', function(obj){
            $.ajax({
                url: '/login/addJsUser',
                dataType: 'json',
                type: 'post',
                data: obj.field,
                success:function (data) {
                    var userId =  data.data.userId;
                    if(data.data.userId != null){
                        if(deptAuthoritys.length > 0){
                            $.ajax({
                                url: '/jsRoleAuthority/addUserAuthoritys',
                                type: 'post',
                                dataType: 'json ',
                                data: {
                                    authorityIds: deptAuthoritys,
                                    userId: userId,
                                    type: 2
                                },
                                success: function (data) {
                                    if(data.data){
                                        $.ajax({
                                            url:"/login/addJsUserDept",
                                            type:"post",
                                            dataType:"json",
                                            data:{'userId':userId,'deptId':obj.field.dept},
                                            success:function (data) {
                                                parent.addTab(86);
                                                location.reload();
                                            }
                                        })
                                    }
                                }
                            });
                        }
                    }
                }
            });
            return false;
            /*var flag=1;
            var param={
                'trueName':$("input[name=trueName]").val(),
                'account':$("input[name=account]").val(),
                'acatarUrl':$("input[name=acatarUrl]").val(),
                'sex':sex,
                'email':$("input[name=email]").val(),
                'password':$("input[name=password]").val(),
                'phone':$("input[name=phone]").val(),
                'idCard':$("input[name=idCard]").val(),
                'birthDate':$("input[name=birthDate]").val(),
                'freestyle':$("input[name=freestyle]").val(),
                'remark':'后台添加',
                'enabled':1,
                'registerDate':getTime(),
                'registerIp':returnCitySN.cip,
                'status':1,
                'grade':1,
                'wages':2000
            }
            $.ajax({
                url:"/login/getJsUserAccount",
                type:"post",
                dataType:"json",
                data:{},
                success:function (data) {
                    for(var i=0;i<data.data.length;i++){
                        if(data.data[i]==$("input[name=account]").val()){
                            flag=2;
                        }
                    }
                    if(flag==1){
                        $.ajax({
                            url:"/login/addJsUser",
                            type:"post",
                            dataType:"json",
                            data:param,
                            success:function (data) {
                                $.ajax({
                                    url:"/login/getJsUserByAccount",
                                    type:"post",
                                    dataType:"json",
                                    data:{'account':$("input[name=account]").val()},
                                    success:function (data) {
                                        $.ajax({
                                            url:"/login/addJsUserDept",
                                            type:"post",
                                            dataType:"json",
                                            data:{'userId':data.data,'deptId':dept},
                                            success:function (data) {
                                                layer.msg("添加成功！！！");
                                                location.reload();
                                                parent.addTab(86);
                                            }
                                        })
                                    }
                                })
                            }
                        })
                    }else{
                        layer.msg("已经存在此账号的用户！！！")
                    }
                }
            })*/
        });

        //现在时间
        function getTime(){
            var date=new Date();
            var year=date.getFullYear();
            var month=date.getMonth()+1;
            var day=date.getDate();
            var hour=date.getHours();
            var minute=date.getMinutes();
            var second=date.getSeconds();
            var dateStr = year+"-"+zeroFilling(month)+"-"+zeroFilling(day)+" "+zeroFilling(hour)+":"+zeroFilling(minute)+":"+zeroFilling(second);
            return dateStr;
        }
        function zeroFilling(data){
            if(data<10){
                data="0"+data;
            }
            return data;
        }

    })
</script>
</html>